package com.cncsi.util;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.regex.Pattern;


/**
 * 多表关联动态sql工具类
 *
 * @author 王玉涛
 * @Param
 * @date 2017-10-21 9:34
 */
@Service
public class QueryUtil {

    private Logger logger = Logger.getLogger(getClass());

    private static Pattern P = Pattern.compile("order\\s*by.*");
    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 查询,把分页进行添加
     */
    public <T> List<T> query(String sql, Class<T> dto, Object[] params) {
        logger.info("SQL=" + sql);
        return jdbcTemplate.query(sql, params, new BeanPropertyRowMapper<T>(dto));
    }

    /**
     * 查询，返回list
     *
     * @param sql 把条件拼装好的sql
     * @param dto 查询的dto.class
     * @return
     */
    public <T> List<T> query(String sql, Class<T> dto) {
        logger.info("SQL=" + sql);
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<T>(dto));
    }


}
